/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package edu.mse.camel.client.whiteboard.gef.whiteboard.impl;

import edu.mse.camel.client.whiteboard.gef.whiteboard.Shape;
import edu.mse.camel.client.whiteboard.gef.whiteboard.WhiteboardDiagram;
import edu.mse.camel.client.whiteboard.gef.whiteboard.FreeWritingShape;
import edu.mse.camel.client.whiteboard.gef.whiteboard.WhiteboardPackage;

import java.util.Collection;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList;
import org.eclipse.emf.ecore.util.EObjectResolvingEList;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.swt.graphics.RGB;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Diagram</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link edu.mse.camel.client.whiteboard.gef.whiteboard.impl.WhiteboardDiagramImpl#getShapes <em>Shapes</em>}</li>
 *   <li>{@link edu.mse.camel.client.whiteboard.gef.whiteboard.impl.WhiteboardDiagramImpl#getLocation <em>Location</em>}</li>
 *   <li>{@link edu.mse.camel.client.whiteboard.gef.whiteboard.impl.WhiteboardDiagramImpl#getSize <em>Size</em>}</li>
 *   <li>{@link edu.mse.camel.client.whiteboard.gef.whiteboard.impl.WhiteboardDiagramImpl#getBackgroundColor <em>Background Color</em>}</li>
 *   <li>{@link edu.mse.camel.client.whiteboard.gef.whiteboard.impl.WhiteboardDiagramImpl#getFreeWritingShapes <em>Free Writing Shapes</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class WhiteboardDiagramImpl extends EObjectImpl implements WhiteboardDiagram {
	/**
	 * The cached value of the '{@link #getShapes() <em>Shapes</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getShapes()
	 * @generated
	 * @ordered
	 */
	protected EList<Shape> shapes;

	/**
	 * The default value of the '{@link #getLocation() <em>Location</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getLocation()
	 * @generated
	 * @ordered
	 */
	protected static final Point LOCATION_EDEFAULT = null;
	/**
	 * The cached value of the '{@link #getLocation() <em>Location</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getLocation()
	 * @generated
	 * @ordered
	 */
	protected Point location = LOCATION_EDEFAULT;
	/**
	 * The default value of the '{@link #getSize() <em>Size</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getSize()
	 * @generated
	 * @ordered
	 */
	protected static final Dimension SIZE_EDEFAULT = null;
	/**
	 * The cached value of the '{@link #getSize() <em>Size</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getSize()
	 * @generated
	 * @ordered
	 */
	protected Dimension size = SIZE_EDEFAULT;
	/**
	 * The default value of the '{@link #getBackgroundColor() <em>Background Color</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getBackgroundColor()
	 * @generated
	 * @ordered
	 */
	protected static final RGB BACKGROUND_COLOR_EDEFAULT = null;
	/**
	 * The cached value of the '{@link #getBackgroundColor() <em>Background Color</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getBackgroundColor()
	 * @generated
	 * @ordered
	 */
	protected RGB backgroundColor = BACKGROUND_COLOR_EDEFAULT;

	/**
	 * The cached value of the '{@link #getFreeWritingShapes() <em>Free Writing Shapes</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFreeWritingShapes()
	 * @generated
	 * @ordered
	 */
	protected EList<FreeWritingShape> freeWritingShapes;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected WhiteboardDiagramImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return WhiteboardPackage.Literals.WHITEBOARD_DIAGRAM;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Shape> getShapes() {
		if (shapes == null) {
			shapes = new EObjectContainmentEList<Shape>(Shape.class, this, WhiteboardPackage.WHITEBOARD_DIAGRAM__SHAPES);
		}
		return shapes;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Point getLocation() {
		return location;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setLocation(Point newLocation) {
		Point oldLocation = location;
		location = newLocation;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, WhiteboardPackage.WHITEBOARD_DIAGRAM__LOCATION, oldLocation, location));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Dimension getSize() {
		return size;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setSize(Dimension newSize) {
		Dimension oldSize = size;
		size = newSize;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, WhiteboardPackage.WHITEBOARD_DIAGRAM__SIZE, oldSize, size));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public RGB getBackgroundColor() {
		return backgroundColor;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setBackgroundColor(RGB newBackgroundColor) {
		RGB oldBackgroundColor = backgroundColor;
		backgroundColor = newBackgroundColor;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, WhiteboardPackage.WHITEBOARD_DIAGRAM__BACKGROUND_COLOR, oldBackgroundColor, backgroundColor));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<FreeWritingShape> getFreeWritingShapes() {
		if (freeWritingShapes == null) {
			freeWritingShapes = new EObjectContainmentEList<FreeWritingShape>(FreeWritingShape.class, this, WhiteboardPackage.WHITEBOARD_DIAGRAM__FREE_WRITING_SHAPES);
		}
		return freeWritingShapes;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__SHAPES:
				return ((InternalEList<?>)getShapes()).basicRemove(otherEnd, msgs);
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__FREE_WRITING_SHAPES:
				return ((InternalEList<?>)getFreeWritingShapes()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__SHAPES:
				return getShapes();
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__LOCATION:
				return getLocation();
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__SIZE:
				return getSize();
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__BACKGROUND_COLOR:
				return getBackgroundColor();
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__FREE_WRITING_SHAPES:
				return getFreeWritingShapes();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__SHAPES:
				getShapes().clear();
				getShapes().addAll((Collection<? extends Shape>)newValue);
				return;
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__LOCATION:
				setLocation((Point)newValue);
				return;
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__SIZE:
				setSize((Dimension)newValue);
				return;
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__BACKGROUND_COLOR:
				setBackgroundColor((RGB)newValue);
				return;
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__FREE_WRITING_SHAPES:
				getFreeWritingShapes().clear();
				getFreeWritingShapes().addAll((Collection<? extends FreeWritingShape>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__SHAPES:
				getShapes().clear();
				return;
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__LOCATION:
				setLocation(LOCATION_EDEFAULT);
				return;
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__SIZE:
				setSize(SIZE_EDEFAULT);
				return;
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__BACKGROUND_COLOR:
				setBackgroundColor(BACKGROUND_COLOR_EDEFAULT);
				return;
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__FREE_WRITING_SHAPES:
				getFreeWritingShapes().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__SHAPES:
				return shapes != null && !shapes.isEmpty();
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__LOCATION:
				return LOCATION_EDEFAULT == null ? location != null : !LOCATION_EDEFAULT.equals(location);
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__SIZE:
				return SIZE_EDEFAULT == null ? size != null : !SIZE_EDEFAULT.equals(size);
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__BACKGROUND_COLOR:
				return BACKGROUND_COLOR_EDEFAULT == null ? backgroundColor != null : !BACKGROUND_COLOR_EDEFAULT.equals(backgroundColor);
			case WhiteboardPackage.WHITEBOARD_DIAGRAM__FREE_WRITING_SHAPES:
				return freeWritingShapes != null && !freeWritingShapes.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (location: ");
		result.append(location);
		result.append(", size: ");
		result.append(size);
		result.append(", BackgroundColor: ");
		result.append(backgroundColor);
		result.append(')');
		return result.toString();
	}
	  public boolean addChild(FreeWritingShape  shape) {
		    {
		        if(shape.getID() == null)
		            shape.setID(generateShapeID());
//		        shape.eAdapters().add(this);
		        return getFreeWritingShapes().add(shape);
		    }
	 }
	  
	
	  
	 private String guid_root = null;
	 private int guid_index = 0;
		
	 public String generateShapeID()
	 {
		if(guid_root == null)
	    	guid_root = ""+System.currentTimeMillis();					
			return guid_root+"."+guid_index++;
	 }
	 
	 public boolean removeChild(FreeWritingShape shape)
	    {
	        shape.eAdapters().remove(this);
	        return getFreeWritingShapes().remove(shape);
	    }

} //WhiteboardDiagramImpl
